package development.socket;

import state.AlertListener;
import state.GameAlert;
import state.GameGlobal;
import state.socket.DataPackage;
import state.util.array.StringArray;
import development.FrmListFreeUser;
import development.FrmLogin;
import development.Global;
import development.bean.Game;
import development.bussiness.GameBussiness;
import development.game.FrmAilaTrieuPhuOff;

public class GameActionHandle extends RequestGroupHandle implements AlertListener {
  public static boolean isOnSignOut = false;
  public static boolean isLoginSuccess = false;

  public GameBussiness gameBussiness;

  public GameActionHandle() {
    gameBussiness = new GameBussiness();
  }

  public void alertEventPerform(int alertType, int eventType, int alertId) {
    // Khi mất kết nối với máy chủ
    if (alertId == 96) {
      GameGlobal.nextState(FrmLogin.instance(), null);
    }
  }

  public void processPackage(DataPackage dataPackage) {
    super.processPackage(dataPackage);
    int header = dataPackage.getHeader();
    switch (header) {

    /** Authenticate */
    case ProtocolConstants.ResponseHeader.SALT_REPONSE:
      String salt = dataPackage.nextString();
      SocketClientUtil.authenToServerBySessionId(Global.gameActionClient, Global.currentUser.getName(), Global.session.getId(), salt);
      break;
    case ProtocolConstants.ResponseHeader.AUTHENTICATE_SUCCESS_RESPONSE:
      gameBussiness.onAuthenticateSuccess(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.AUTHENTICATE_FAIL_RESPONSE:
      gameBussiness.onAuthenticateFail(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.GAME_SERVER_FULL_RESPONSE:
      GameGlobal.alert.showAlert(this, GameAlert.OK_TYPE, "Server đã đầy");
      break;

    /** Game, room and table */
    case ProtocolConstants.ResponseHeader.ROOM_LIST_RESPONSE:
      gameBussiness.getRoomList(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.TABLE_LIST_RESPONSE:
      gameBussiness.getTableList(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.JOIN_GAME_SUCCESS_RESPONSE:
      gameBussiness.getRoomList(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.ACCEPT_GAME_SUCCESS_RESPONSE:
      gameBussiness.getRoomList(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.GAME_USER_COUNT_RESPONSE:
      gameBussiness.onGameUserCountReceive(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.JOIN_GAME_FAIL_RESPONSE:
      break;
    case ProtocolConstants.ResponseHeader.JOIN_ROOM_SUCCESS_RESPONSE:
      gameBussiness.getTableList(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.ACCEPT_ROOM_SUCCESS_RESPONSE:
      gameBussiness.getTableList(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.JOIN_ROOM_FAIL_RESPONSE:
      gameBussiness.onJoinRoomFail(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.JOIN_TABLE_SUCCESS_RESPONSE:
      gameBussiness.onJoinTableSuccess(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.JOIN_TABLE_FAIL_RESPONSE:
      gameBussiness.onJoinTableFail(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.USER_LIST_OF_TABLE_RESPONSE:
      gameBussiness.getTableUserList(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.CONFIG_TABLE_SUCCESS_RESPONSE:
      gameBussiness.onConfigTableSuccess(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.TABLE_CONFIG_CHANGE_RESPONSE:
      gameBussiness.onConfigTableChange(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.CONFIG_TABLE_FAIL_RESPONSE:
      gameBussiness.onConfigTableFail(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.GAME_START_RESPONSE:
      gameBussiness.onStartGame();
      break;
    case ProtocolConstants.ResponseHeader.GAME_START_FAIL_RESPONSE:
      gameBussiness.onGameStartFail(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.LIST_USER_WANT_TO_PLAY_RESPONSE:
      gameBussiness.onListUsersWantToPlay(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.INVITE_USER_TO_TABLE_RESPONSE:
      gameBussiness.onInviteUserToTable(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.INVITE_USER_TO_TABLE_FAIL_RESPONSE:
      gameBussiness.onInviteUserFail(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.JOIN_TABLE_ACCEPT_RESPONSE:
      gameBussiness.onAcceptInviteUserToTable(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.AUTO_FIND_TABLE_RESPONSE:
      gameBussiness.onAutoFindTable(dataPackage);
      break;
      
    case ProtocolConstants.ErrorCode.NO_MORE_FREE_USER:
      FrmListFreeUser frmListFreeUser = (FrmListFreeUser) FrmListFreeUser.instance();
      frmListFreeUser.isReceivedData = true;
      break;
    case ProtocolConstants.ErrorCode.NO_MORE_FREE_TABLE:
      GameGlobal.alert.showAlert3(this, GameAlert.OK_TYPE, StringArray.create1( "Không có bàn nào trống. ", "Hãy thử lại sau." ));
      break;
      
    case ProtocolConstants.ResponseHeader.NOT_ENOUGH_MONEY_TO_CONTINUE_PLAY_RESPONSE:
      gameBussiness.onNotEnoughMoneyToContinuePlayGame(dataPackage);
      break;
      
    case ProtocolConstants.ResponseHeader.KICK_USER_SUCCESS_RESPONSE:
      gameBussiness.onKickUserSuccess(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.KICK_USER_FAIL_RESPONSE:
      gameBussiness.onKickUserFail(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.KICK_OUT_OF_TABLE_RESPONSE:
      gameBussiness.onKickedOutOfTable(dataPackage);
      break;
      
    /** Auto invite game*/
    case ProtocolConstants.ResponseHeader.AUTO_INVITE_GAME_NOTICE_REPONSE:
      gameBussiness.onAutoNoticeInviteGame(dataPackage);
      break;
    case ProtocolConstants.ResponseHeader.AUTO_INVITE_LIST_USER_RESPONSE:
      gameBussiness.onAutoInviteGameListUser(dataPackage);
      break;
      
      
      
    /** Game Action */
    case ProtocolConstants.ResponseHeader.GAME_ACTION_RESPONSE:
      Global.currentGame.onRecieveData(Game.getActionPackage(dataPackage));
      break;
      
    case ProtocolConstants.ResponseHeader.GAME_AI_LA_TRIEU_PHU_OFF_ACTION_RESPONSE:
    	((FrmAilaTrieuPhuOff) FrmAilaTrieuPhuOff.instance()).onRecieveDataProcess(dataPackage);
    	break;
    case ProtocolConstants.ResponseHeader.GAME_DUOI_HINH_BAT_CHU_OFF_ACTION_RESPONSE:
      // TODO: disable game DHBC off
//    	((FrmDuoiHinhBatChuOff) FrmDuoiHinhBatChuOff.instance()).onRecieveDataProcess(dataPackage);
    	break;
    case ProtocolConstants.ResponseHeader.NOT_ENOUGH_MONEY_TO_PLAY_GAME_OFF_RESPONSE:
      gameBussiness.onNotEnoughMoneyToPlayGameOff(dataPackage);
      break;
    default:
      System.err.println("GameActionHandle : Receive unknown message header " + header);
      break;
    }
  }

  public void onConnectDone() {
    super.onConnectDone();
    SocketClientUtil.startAuthenToServer(Global.gameActionClient, Global.currentUser.getName());
  }

  public void onConnectFail() {
    super.onConnectFail();
    GameGlobal.alert.showAlert(this, GameAlert.OK_TYPE, "Kết nối đến server game thất bại");
  }

  public void onDisconnect() {
    super.onDisconnect();
  }
}
